package com.ydgk.service;

import com.ydgk.bean.User;
import com.ydgk.dao.UserDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {
    private  UserDao userDao=new UserDao();
    public Map AdminSearch(String phone, Integer pageNumber){
        int maxPage = userDao.adminSearchMaxPage(phone);
        if(pageNumber > maxPage){
            pageNumber = maxPage;
        }
        if(pageNumber < 1){
            pageNumber = 1;
        }
        String pageCode = userDao.genPagination
                ("UserServlet.action",maxPage,pageNumber,"flag=findUser&phone="+(phone==null?"":phone));

        List<User> userList = userDao.adminSearch(phone , pageNumber);

        HashMap map = new HashMap();
        map.put("pageCode",pageCode);
        map.put("userList",userList);
        return map;

    }
    public List<User> findAll(){
        return userDao.findAll();
    }

    public int saveOrUpdate(User user){

        if(user.getId() != null && user.getId() != 0 ){
            //修改
            return userDao.updateById(user);
        }else{
            //添加
            return userDao.save(user);
        }
    }
    public int deleteById(int id){
        return userDao.deleteById(id);
    }
}
